package com.prs.crm.filter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.prs.crm.exception.InValidException;

public class SysFilter implements Filter{

	private FilterConfig config;
	
	public void init(FilterConfig config)
	{
		this.config = config;
	}
	
	private String getErrorURI()
	{
		return config.getServletContext().getContextPath()+"/error.jsp";
	}
	
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
	{
		try
		{
			chain.doFilter(request, response);

		}
		catch (InValidException e)
		{
			try
			{
				request.setAttribute("refer", 	((HttpServletRequest)request).getHeader("Referer"));
				request.setAttribute("exception", e.getMessage());
				((HttpServletRequest)request).getRequestDispatcher(getErrorURI()).forward(request, response);
			}
			catch (Exception exx)
			{
				e.printStackTrace();
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			request.setAttribute("exception", ex.getMessage());
		}
	}
	
	public void destroy()
	{
		
	}
}
